package com.sh.personnel.security.config.handler;

import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;

/**
 * 自定义的接口回调处理器
 * @author 林思浩
 * @date 2020/01/05 21:32
 */
public class MyCustomHandler {

    /**
     * 返回json格式信息
     * @param status
     * @param message
     * @param response
     * @throws IOException
     */
    public void responseJson(Object status, Object message, HttpServletResponse response) throws IOException {
        response.setContentType("application/json;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        response.setStatus((Integer) status);
        PrintWriter out = response.getWriter();
        HashMap<String, Object> map = new HashMap<>();
        map.put("code", status);
        map.put("message", message);
        ObjectMapper om = new ObjectMapper();
        out.write(om.writeValueAsString(map));
        out.flush();
        out.close();
    }

}
